#!perl
use LWP::Simple;
use LWP::UserAgent;
use threads;


if ($0=~m#^(.*)\\#) {
	    $dir = "$1";
	} elsif ($0=~m#^(.*)/# ) {
	    $dir = "$1";
	} else  {`pwd` =~ /(.*)/;
    	$dir = "$1";
	}

$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
$ua->cookie_jar({ file => "C:/cookies.txt" });


open(OUT, '>status.log') or die $!;
my $site == 1;
while ($choice != 6){
#top:
print "\n\tMenu\n
--------------------\n
1. Login\n
2. View \'My\' Playlists\n
3. Enter playlist ID\n
4. Search for songs\n
5. Show result from last search again\n
6. Exit\n
Enter choice: ";

$choice = <STDIN>;print "\n";

$playlist = &login() if $choice == 1;
&showPL($playlist) if $choice == 2;
&dlPlaylist(1) if $choice == 3;
&dlSearch() if $choice == 4;
&findSongs() if $choice == 5;
}

sub login {
	print "Email address: ";
	$login = <STDIN>;
	chomp $login;
	print "\nPassword: ";
	$pass = <STDIN>;
	chomp $pass;
	$rs = &randString(9);
	my $req = HTTP::Request->new(POST => 'https://www.seeqpod.com/cgi-bin/auth/https_secure/login');
	$req->content_type('application/x-www-form-urlencoded');
	@login = split /\@/, $login;
	$login = @login[0] . "%40" . @login[1];
	@login = split /\./, $login;
	$login = @login[0] . "%2E" . @login[1];
	$content = "password=$pass&remember%5Fme=true&random=$rs&email=$login";
	$req->content($content);
	my $res = $ua->request($req);
	
	$rs = &randString(9);
	$plURL = "http://www.seeqpod.com/api/music/secure_getPlaylistIds?random=$rs";
	$response = $ua->get($plURL);
	return $response->content . "\n";
}

sub showPL {
	@plIDs = @_[0] =~ /\<playlist_id\>(.*?)\<\/playlist_id\>/g;
	@plNames = @_[0] =~ /\<playlist_name\>(.*?)\<\/playlist_name\>/g;

	for($i = 0; $i < @plNames; $i++){
		print "".($i+1) . ". ";
		print @plNames[$i] . "\n";
	}
	print "Which one would you like to see: ";
	$plChoice = <STDIN>;
	chomp $plChoice;
	$plChoice--;
	$plChoice = @plIDs[$plChoice];
	&dlPlaylist(0,$plChoice);
}

sub dlPlaylist {
	$playlistID = @_[1];
	print "Enter a playlist ID: " if @_[0] == 1;
	$playlistID = <STDIN> if @_[0] == 1;
	chomp($playlistID) if @_[0] == 1;
	$rs = randString(9);
	$playlistURL = "http://www.seeqpod.com/api/music/getPlaylist?playlist_id=$playlistID&v=2&random=$rs";
	$xml = get $playlistURL;
	&findSongs();
}

sub dlSearch {
	print "Enter search terms: ";
	$search = <STDIN>;
	chomp($search);
	$rs = randString(9);
	$searchURL = "http://www.seeqpod.com/api/seeq/search?rt=0&s=0&n=30&q=$search&rv=0&rm=1&rp=0&random=$rs";
	$xml = get $searchURL;
	&findSongs();
}

sub findSongs {
	$xml =~ /\<name\>(.*)\<\/name\>/;
	$playlistName = $1;
	@songs = $xml =~ /\<title\>(.*?)\<\/title\>/g;
	@artists = $xml =~ /\<creator\>(.*?)\<\/creator\>|.*?\<creator\/\>/g;

	print "\n\t\tSongs\n----Enter the song numbers or the word all----\n";
	for($i = 0; $i < @songs; $i++){
		print "".($i+1) . ". ";
		print @artists[$i] . " - " if (@artists[$i]);
		print @songs[$i] . "\n";
	}
	print "Enter song numbers, or type \'cancel\' (separate song numbers with spaces OR use the word \'all\'): ";
	$choices = <STDIN>;
	chomp ($choices);
	$choices = lc($choices);
	return if $choices == "cancel";

	@downloadIDs = $xml =~ /\<mp3_url_id\>(.*?)\<\/mp3_url_id\>/g;
	@locations = $xml =~ /\<location\>(.*?)\<\/location\>/g;

	print "Downloading chosen songs in $playlistName...\n";

	print "----Save to where----\n";
	print "1. Directory the program is in (default)\n";
	print "2. Enter directory\n";
	print "3. Cancel\n";
	print "Enter choice: ";
	$dirChoice = <STDIN>;
	chomp $dirChoice;
	return if $dirChoice == "3";

	if ($0=~m#^(.*)\\#) {
	    $dir = "$1";
	} elsif ($0=~m#^(.*)/# ) {
	    $dir = "$1";
	} else  {`pwd` =~ /(.*)/;
    	$dir = "$1";
	}
	if ($dirChoice == 2){
		print "\nEnter the directory to save files in: ";
		$dir = <STDIN>;
		chomp $dir;
	}
	print "\nSaving songs to $dir\n";

	if ($choices != "all"){
	@chosenSongs = split (/ /,$choices);
	for ($j = 0; $j < @chosenSongs; $j++){
		$temp = (@chosenSongs[$j]-1);
		@dl = &prepDL(@songs[$temp],@downloadIDs[$temp]);
		threads->create(\&trackDL,@dl[2],@dl[0],@songs[$temp])->detach();
		threads->create(\&getSong,@dl[1],@dl[0])->detach();
	}}
	
	if ($choices == "all"){
	for ($j = 0; $j < @songs; $j++){
		@dl = &prepDL(@songs[$j],@downloadIDs[$j]);
		threads->create(\&trackDL,@dl[2],@dl[0],@songs[$j])->detach();
		threads->create(\&getSong,@dl[1],@dl[0],@dl[2])->detach();
	}
	}
}

sub prepDL {
	$dlSave = $dir . "\\" . @_[0] . ".mp3";
	open(MP3, ">$dlSave");
	close(MP3);
	$rs = &randString(9);
	$dlURL = "http://www.seeqpod.com/api/youtube.com/results?mp3_url_id=@_[1]&random=$rs";
	$dlURL = get $dlURL;
	#if (! (head $dlURL)){print "The link doesn't work \n";unlink $dlSave;goto top;}
	@head = head $dlURL;$size = @head[1];
	return ($dlSave,$dlURL,$size);
}

sub getSong {
	print "Saving @_[1]\n";
	$sTime = time;
	$rc = getstore @_[0], @_[1];
	$tTime = (time-$sTime);
	$speed = $size/$tTime*.0009765625 if $size != 0;
	$speed = sprintf("%.2f", $speed);
	print OUT "Finished saving: @_[1]. Took $tTime seconds ($speed Kbps)\n" if (is_success($rc));
}

sub trackDL {
	$sTime = time;
	$total = @_[0];
	$tMB = ($total*.0009765625*.0009765625);
	$current = -s @_[1];
	while($total != $current){
		$current = -s @_[1];
		$percent = ($current/$total*100) if $current != 0;
		$speed = $current/(time-$sTime)*.0009765625 if $current != 0;
		$speed = 0 if ($current*.0009765625*.0009765625) == $cMB;
		$cMB = ($current*.0009765625*.0009765625);
		$tRemain = ($total-$current)/($current/(time-$sTime)) if $current != 0;
		$tRemain = -1 if $speed == 0;
		$ftr = localtime($tRemain);
		printf OUT "%s: %.2f%% Done (%.2f out of %.2f MB total ~ %.2f Kbps) Time remaining: ~ %.0f sec\n",@_[2],$percent,$cMB,$tMB,$speed,$tRemain;
		sleep 1;
	}
}

sub randString
{
	my $length_of_randomstring=shift;# the length of 
			 # the random string to generate

	my @chars=('a'..'z','A'..'Z','0'..'9');
	my $random_string;
	foreach (1..$length_of_randomstring) 
	{
		# rand @chars will generate a random 
		# number between 0 and scalar @chars
		$random_string.=$chars[rand @chars];
	}
	return $random_string;
}
close(OUT);

#2d40e9106a